package com.example.deertrip;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



import androidx.appcompat.app.AppCompatActivity;

import com.example.deertrip.bean.Tb_HotelInfo;
import com.example.deertrip.database.DBHleper;
import com.example.deertrip.database.DBOperate;

import java.util.ArrayList;
import java.util.List;

public class login_user_Activity extends AppCompatActivity {
    private DBHleper dbhelper;
    private Context context;
    private DBOperate dbOperate;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_user);


        EditText username;
        EditText password;
        username=(EditText) findViewById(R.id.login_name_denglu2);
        password=(EditText) findViewById(R.id.login_password_denglu2);

        Button user_denglu1;
        Button user_zhuce1;
        user_denglu1=(Button) findViewById(R.id.login_user_denglu);
        user_zhuce1=(Button) findViewById(R.id.login_user_zhuce);



        user_denglu1.setOnClickListener(new View.OnClickListener() {
            @Override
            @SuppressLint("Range")
            public void onClick(View view) {

                String name=username.getText().toString();
                System.out.println(name);
                String pass=password.getText().toString();
                System.out.println(pass);

                //   Log.i("TAG",name+"_"+pass);
                DBOperate dboperate=new DBOperate(login_user_Activity.this);
                boolean flag=dboperate.userlogin(name, pass);
                if(flag){
                    SharedPreferences sp=getSharedPreferences("deertrip",MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putInt("login_state",1);
                    editor.commit();

                    List<Tb_HotelInfo> list = new ArrayList<Tb_HotelInfo>();

                    SQLiteDatabase db=dboperate.dbhelper.getReadableDatabase();
                    String sql="select * from Tb_UserInfo where UserName=? and UserPassword=?";
                    Cursor cursor=db.rawQuery(sql, new String[]{name, pass});
                    if(cursor.moveToFirst()) {
                        SharedPreferences userInfo = getSharedPreferences("deertrip", MODE_PRIVATE);
                        SharedPreferences.Editor usereditor = userInfo.edit(); // 获取编辑器
                        editor.putString("UserName", cursor.getString(cursor.getColumnIndex("UserName")));         // 存入String类型数据
                        editor.putString("UserAddress", cursor.getString(cursor.getColumnIndex("UserAddress")));         // 存入String类型数据
                        editor.putString("UserTel", cursor.getString(cursor.getColumnIndex("UserTel")));         // 存入String类型数据
                        editor.putString("WxBalance", cursor.getString(cursor.getColumnIndex("WxBalance")));
                        editor.putString("YhkBalance", cursor.getString(cursor.getColumnIndex("YhkBalance")));
                        editor.putString("ZfbBalance", cursor.getString(cursor.getColumnIndex("ZfbBalance")));
                        editor.commit();                             // 提交修改
                    }

                    Log.i("TAG","登录成功");
                    Toast.makeText(login_user_Activity.this, "登录成功", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(login_user_Activity.this,WelcomeActivity.class);
                    startActivity(intent);
                }else{
                    Log.i("TAG","登录失败");
                    Toast.makeText(login_user_Activity.this, "登录失败", Toast.LENGTH_LONG).show();
                }

            }
        });

        user_zhuce1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2=new Intent( login_user_Activity.this, login_useregister_activity.class);
                startActivity(intent2);
            }
        });

    }
}

